package iterator;

/**
 * <p>Title BookkShelfIterator</p>
 *
 * @author YiMing Liu
 * @date 2018/6/7 16:00
 * <p>Description:书架迭代器</p>
 */
public class BookkShelfIterator implements Iterator {

    private BookShelf bookShelf;
    private int index;

    public BookkShelfIterator(BookShelf bookShelf) {
        this.bookShelf = bookShelf;
        this.index = 0;
    }

    /**
     * 判断是否有下一个元素
     * @return
     */
    @Override
    public boolean hasNext() {
        if (index < bookShelf.getLength()) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 返回当前元素并且指向下一个元素
     * @return
     */
    @Override
    public Object next() {
        Book book = bookShelf.getBookAt(index);
        index++;
        return book;
    }
}
